/*
 *	form.html5.js - copyright 2004-2010, GuCong
 *	http://gucong.co.cc/textbox/
 *	http://www.gnu.org/licenses/gpl.html
 */

(function(win, doc, $, undefined){
	if($("<input>").prop("oninvalid") === undefined){

		var html = doc.documentElement;

		function validate(e){
			$(e.target).not("[novalidate]").find("[pattern],[required]").each(function(i, input) {
				var value = input.value;
				input = $(input);
				if(
					value ?
						(function(){
							var regexp = input.attr("pattern");
							if(regexp){
								return !new RegExp( "^" + regexp + "$").test(value);
							}
						})()
					:
					input.is("[required]")
				){
					e.preventDefault();
					e.stopPropagation();
					setTimeout(function(){
						try{
							input.trigger("invalid");
						}catch(ex){}
					},1);
				}
			});
			if(e.isDefaultPrevented()){
				return false;
			}
		}

		try{
			html.addEventListener("submit", validate , true );
		}catch(ex){
			doc.forms.onvalidate = validate;
		}

	}

})(this, this.document, this.jQuery);

/*@cc_on @if (@_jscript)
(function(win, doc){
	var mode = doc.documentMode || 7,
		style = doc.createStyleSheet("", 0),
		path = doc.scripts;
	path = path[path.length - 1];
	path = path.src;
	path = path.substr(0, path.lastIndexOf("/") + 1);
	if(mode < 10){
		function addhtc(node, file){
			style.addRule(node, "behavior: url(" + path + file + ")");
		}
		addhtc("input", "input.html5.htc");
		if(mode < 9){
			style.addRule("input", "vertical-align: middle");
			addhtc("form", "form.html5.htc");
			if( mode < 8 ){
				style.addRule("html", "_overflow-y: expression(scrollHeight > offsetHeight ? 'scroll' : 'hidden')");
				doc.execCommand("BackgroundImageCache", false, true);
				addhtc("select", "select.htc");
			}
		}
	}
})(this, this.document);
@else @*/
(function(win, $, undefined){
	
	//请在这里修改placeholder的字体颜色
	var color = "darkGray";
	
	if($("<input>").prop("placeholder") == undefined) {
		function setFocus(input){
			setTimeout( function(){
				$(input).focus();
			}, 2);
		}

		function placeholder(input){
			var text = input.getAttribute("placeholder") || "",
				place = $.data(input, "placeholder"),
				holder;
			if(place){
				holder = place.children();
			}else{
				holder = $("<placeholder>");
				place = holder.clone().mousedown(function(){
					setFocus(input);
				}).append(holder);
				$.data(input, "placeholder", place);

				$(input).bind("blur.placeholder", function(){
					set();
					holder.show();
				}).bind("focus.placeholder", function(){
					holder.hide();
				}).bind("change.placeholder", set);
	
			}
			input = $(input);
			function set(){
				if(text && !input.val()){
					holder.html(text).css({
						paddingLeft: input.css("paddingLeft"),
						marginLeft: input.css("marginLeft"),
						left: input.css("borderLeftWidth"),
						verticalAlign: "center",
						whiteSpace: "nowrap",
						position: "absolute",
						overflow: "hidden"
					});
					var height = input.css("height")
					place.css({
						fontSize: input.css("fontSize"),
						verticalAlign: "middle",
						display: "inline-block",
						position: "relative",
						lineHeight: height,
						height: height,
						color: color
					});
					input.before(place);
				} else {
					place.hide();
				}
			}
			setTimeout( set, 1);
		}
		
		$.attrHooks.placeholder = $.propHooks.placeholder = {
			set: function(element, value){
				element.setAttribute("placeholder", value);
				placeholder(element);
				return value;
			},
			get: function(element){
				placeholder(element);
				return element.getAttribute("placeholder");
			}
		}

		$(function(){
			$("[placeholder]").each(function(index, element) {
				placeholder(element);
			});
			setFocus("[autofocus]");
		});
	} else {
		color = "placeholder{color:" + color + ";}";
		var style = ":focus::-webkit-input-placeholder{color:transparent !important;}";
		$.each([":-webkit-input-","-webkit-","-moz-",":-ms-",":-o-",""],function(i, name){
			style += ":" + name + color;
		});
		$(document.documentElement).prepend($("<style>").html(style));
	}
})(this, this.jQuery);

/*@end @*/
